package com.jgp.glmeeting.controller;

import com.jgp.glmeeting.model.Invoice;
import com.jgp.glmeeting.service.InvoiceService;
import com.jgp.glmeeting.service.MeetingService;
import com.jgp.glmeeting.service.ParticipantsService;
import com.jgp.glmeeting.util.ExcelUtil;
import com.jgp.sys.common.controller.JGPController;
import com.jgp.sys.ui.Result;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;


@RestController
@RequestMapping("/glMeeting/reportFormApiController")
public class ReportFormApiController extends JGPController {

   private final Logger logger=LoggerFactory.getLogger(ReportFormApiController.class);
    @Autowired
    private InvoiceService invoiceService;

    @Autowired
    private MeetingService meetingService;

    @Autowired
    private ParticipantsService participantsService;

    /**
     * 导出报表
     *
     * @return
     */
    @RequestMapping("/excelPort")
    public void export(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取数据
        List<Invoice> invoiceList = invoiceService.queryAll();

        //excel标题
        String[] title = {"会议名称", "参会人", "联系电话", "入住酒店", "宾客", "所属单位", "发票台头", "组织结构代码", "入住时间"};

        //excel文件名
        String fileName = "发票信息表" + System.currentTimeMillis() + ".xls";

        //sheet名
        String sheetName = "发票信息表";
        String[][] content = new String[title.length][9];
        for (int i = 0; i < invoiceList.size(); i++) {

            Invoice obj = invoiceList.get(i);
           // logger.error((meetingService.queryID(obj.getMeetingId())).getName());
            content[i][0] = (meetingService.queryID(obj.getMeetingId())).getName();
            content[i][1] = (participantsService.queryById(obj.getParticipantsId())).getName();
            content[i][2] = obj.getMobilePhone();
            content[i][3] = obj.getHotel();
            content[i][4] = obj.getParticipantName();
            content[i][5] = obj.getWorkUnit();
            content[i][6] = obj.getInvoiceHead();
            content[i][7] = obj.getOrganizationCode();
            content[i][8] = obj.getCheckInTime();
        }

//创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);

//响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //发送响应流方法
    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(), "ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}